package com.tuniondata.netty.server;

import com.tuniondata.netty.bean.ResponseCodeConstant;
import com.tuniondata.netty.bean.ResponseDTO;
import com.tuniondata.netty.bean.RpcMessage;
import com.tuniondata.netty.common.protocol.Protocol;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * @author yuxinchao
 */
public class ApiProcessor {

    private static final Logger log = LoggerFactory.getLogger(ApiProcessor.class);

    RpcMessage process(RpcMessage rpcMessage) throws Exception {
        RpcMessage res = new RpcMessage();
        res.setRequestId(rpcMessage.getRequestId());
        Protocol protocol = rpcMessage.getProtocol();
        switch (protocol.getMessageType()) {
            // 文件上传请求报文
            case "4001": {
                log.info("文件上传请求报文4001，服务器发送应答报文4008");
                ResponseDTO responseDTO = new ResponseDTO();
                responseDTO.setCode("00");
                responseDTO.setMessage(ResponseCodeConstant.getMessage("00"));
                Protocol responseProtocol = new Protocol();
                responseProtocol.unpack(responseProtocol.pack(responseDTO));
                res.setProtocol(responseProtocol);
                break;
            }
            // 文件数通知报文
            case "4006": {
                log.info("文件数通知报文4006，服务器发送应答报文4008");
                ResponseDTO responseDTO = new ResponseDTO();
                responseDTO.setCode("00");
                responseDTO.setMessage(ResponseCodeConstant.getMessage("00"));
                Protocol responseProtocol = new Protocol();
                responseProtocol.unpack(responseProtocol.pack(responseDTO));
                res.setProtocol(responseProtocol);
                break;
            }
            // 文件信息通知报文
            case "4003": {
                log.info("文件信息通知报文4003，服务器发送断点通知报文4005");
                ResponseDTO responseDTO = new ResponseDTO();
                responseDTO.setCode("00");
                responseDTO.setMessage(ResponseCodeConstant.getMessage("00"));
                Protocol responseProtocol = new Protocol();
                responseProtocol.unpack(responseProtocol.pack(responseDTO));
                res.setProtocol(responseProtocol);
                break;
            }
            // 数据报文
            case "4004": {
                log.info("数据报文4004，服务器发送应答报文4008");
                ResponseDTO responseDTO = new ResponseDTO();
                responseDTO.setCode("00");
                responseDTO.setMessage(ResponseCodeConstant.getMessage("00"));
                Protocol responseProtocol = new Protocol();
                responseProtocol.unpack(responseProtocol.pack(responseDTO));
                res.setProtocol(responseProtocol);
                break;
            }
            // 文件结束报文
            case "4007": {
                log.info("文件结束报文4007，服务器发送应答报文4008");
                ResponseDTO responseDTO = new ResponseDTO();
                responseDTO.setCode("00");
                responseDTO.setMessage(ResponseCodeConstant.getMessage("00"));
                Protocol responseProtocol = new Protocol();
                responseProtocol.unpack(responseProtocol.pack(responseDTO));
                res.setProtocol(responseProtocol);
                break;
            }
            // 应答报文
            case "4008": {
                // TODO
                log.info("应答报文4008");
                break;
            }
            // 断点通知报文
            case "4005": {
                // TODO
                break;
            }
        }

        return res;
    }
}
